package com.controller;

import com.controller.ChatWebSocket;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * WebSocket测试控制器
 * 提供WebSocket相关的测试接口
 */
@RestController
@RequestMapping("/api/websocket")
public class WebSocketTestController {

    /**
     * 获取当前在线用户数
     */
    @GetMapping("/online-count")
    public Map<String, Object> getOnlineCount() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", ChatWebSocket.getOnlineUserCount());
        return result;
    }

    /**
     * 获取所有在线用户
     */
    @GetMapping("/online-users")
    public Map<String, Object> getOnlineUsers() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", ChatWebSocket.getOnlineUsers());
        return result;
    }

    /**
     * 发送消息给指定用户
     */
    @PostMapping("/send-message")
    public Map<String, Object> sendMessage(@RequestParam String userId, @RequestParam String message) {
        Map<String, Object> result = new HashMap<>();
        
        boolean success = ChatWebSocket.sendMessageToUser(userId, message);
        if (success) {
            result.put("code", 200);
            result.put("message", "消息发送成功");
        } else {
            result.put("code", 500);
            result.put("message", "消息发送失败，用户可能不在线");
        }
        
        return result;
    }

    /**
     * 获取WebSocket连接信息
     */
    @GetMapping("/info")
    public Map<String, Object> getWebSocketInfo() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        
        data.put("endpoint", "/ws/chat/{userId}");
        data.put("onlineCount", ChatWebSocket.getOnlineUserCount());
        data.put("onlineUsers", ChatWebSocket.getOnlineUsers());
        
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", data);
        
        return result;
    }
}
